package com.cos.shop.services.impl;

import com.cos.shop.common.Const;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.dao.AddressDao;
import com.cos.shop.dao.impl.AddressDaoImpl;
import com.cos.shop.entity.Address;
import com.cos.shop.services.AddressServices;

import java.sql.SQLException;

/**
 * @author: COS
 * @time: 2023/5/11 15:24
 * @description:
 */
public class AddressServicesImpl implements AddressServices {
    private AddressDao ad = new AddressDaoImpl();

    @Override
    public ResponseResult changeDefault(int aid,int uid) {
        try {
            // 设置地址为默认地址
            // 默认地址只能有1个
            //1 把用户所有地址 都设置为非默认
            ad.setUserAddressNotDefault(uid);
            //2 单独设置 aid这个地址为默认
            ad.setUserAddressDefault(aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createSuccess();
    }

    @Override
    public ResponseResult updateAddress(Address a) {
        try {
            // 判断 啊bnull
            if (a != null) {
                //访问dao新增
                if (ad.updateAddress(a)>0) {
                    return ResponseResult.createSuccess("地址修改成功");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("地址修改失败 哈哈哈");
    }

    @Override
    public ResponseResult insertAddress(Address a) {
        try {
            // 判断 啊bnull
            if (a != null) {
                //设置新添加的地址普通地址
                a.setAstate(Const.NOT_DEFAULT_ADDRESS);
                //访问dao新增
                if (ad.insertAddress(a)>0) {
                    return ResponseResult.createSuccess("地址添加成功");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("地址添加失败 哈哈哈");
    }

    @Override
    public ResponseResult delete(int id) {
        try {
            if (id>0) {
                if (ad.delete(id)>0) {
                    return ResponseResult.createSuccess("删除成功");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("删除失败");
    }

    @Override
    public ResponseResult getAll(int uid) {
        try {
            if (uid>0) {
                return ResponseResult.createSuccess(ad.getAll(uid));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
